package com.jy.rhin.facade.infra.controller.document;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.domain.document.validation.DocumentValidationService;
import com.jy.rhin.infra.repo.document.validation.query.DocumentValidateQueryHandler;
import com.jy.rhin.infra.repo.document.validation.query.DocumentValidationQueryHandler;
import com.jy.rhin.sdk.dto.document.DocumentValidationFileDto;
import com.jy.rhin.sdk.dto.document.query.DocumentValidateResultDto;
import com.jy.rhin.sdk.dto.document.query.DocumentValidationFileRespDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 */
@RestController
@RequestMapping("/infra/document/validation")
@Api(tags = "校验api")
@Slf4j
public class DocumentValidationController {

    @Autowired
    private DocumentValidationService documentValidationFileService;
    @Autowired
    private DocumentValidateQueryHandler documentValidateQueryHandler;

    @Autowired
    private DocumentValidationQueryHandler documentValidationQueryHandler;

    @ApiOperation("查询")
    @GetMapping("/getValidationFilePage")
    public Result<com.jy.framework.base.entity.Page<DocumentValidationFileDto>> getValidationFilePage(DocumentValidationFileRespDto dto) {
        return Result.success(documentValidationQueryHandler.getValidationFilePage(dto));
    }


    @ApiOperation(value = "导入校验文件", notes = "导入校验文件")
    @GetMapping("/importValidationFile")
    public Result importValidationFile(@RequestParam("ossUrl") String ossUrl) throws Exception {
        documentValidationFileService.importValidationFile(ossUrl);
        return  Result.success();
    }


    @ApiOperation(value = "修改校验文件", notes = "修改校验文件")
    @GetMapping("/updateValidationFile")
    public Result updateValidationFile(@RequestBody DocumentValidationFileDto dto) throws Exception {
        documentValidationFileService.updateValidationFile(dto);
        return  Result.success();
    }

    @ApiOperation(value = "校验结果分页", notes = "校验结果分页")
    @GetMapping("/resultPage")
    @ApiImplicitParams({@ApiImplicitParam(name = "documentId", value = "文档id", required = false),
        @ApiImplicitParam(name = "documentType", value = "文档类型", required= false),
        @ApiImplicitParam(name = "kaiShiSJ", value = "开始时间"),
        @ApiImplicitParam(name = "jieShuSJ", value = "结束时间"),
        @ApiImplicitParam(name = "validateResult", value = "校验结果 1-成功 0-失败", required= false),
        @ApiImplicitParam(name = "pageNo", value = "当前页码"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量")})
    public Result<Page<DocumentValidateResultDto>> resultPage( @RequestParam(value = "documentId", required = false) String documentId,
            @RequestParam(value = "documentType", required = false) String documentType,
            @RequestParam(value = "kaiShiSJ") String kaiShiSJ,
            @RequestParam(value = "jieShuSJ" ) String jieShuSJ,
            @RequestParam(value = "validateResult", required = false ) String validateResult,
            @RequestParam(value = "pageNo", defaultValue = "1") long pageNo,
            @RequestParam(value = "pageSize", defaultValue = "10") long pageSize) {
        return  Result.success(documentValidateQueryHandler.resultPage(documentId, documentType, kaiShiSJ, jieShuSJ, validateResult, pageNo, pageSize));
    }

    @ApiOperation(value = "校验结果详情", notes = "校验结果详情")
    @ApiImplicitParam(name = "id", value = "id")
    @GetMapping("/resultDetail")
    public Result<DocumentValidateResultDto> resultDetail( @RequestParam Long id) {
        return  Result.success(documentValidateQueryHandler.resultDetail(id));
    }

    @ApiOperation(value = "根据文档类型获取规则总数", notes = "根据文档名称获取规则总数")
    @ApiImplicitParam(name = "fileType", value = "fileType")
    @GetMapping("/getRuleNumByFileType")
    public Result<Integer> getRuleNumByFileType( @RequestParam("fileType") String fileType) {
        return Result.success(documentValidateQueryHandler.getRuleNumByFileType(fileType));
    }
}
